﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using crm_library.DataAccess.TableReps;
using System.Web.UI.WebControls;
using crm_library.DataAccess;
using System.Data.SqlClient;
using System.Data.Common;
using crm_library.DataAccess.ObjectReps;
using crm_core.List;
using System.Runtime.Serialization;

namespace crm_dal.Objects
{
	[DataContractAttribute]
	public class Contact : ObjectRepresentation
	{
		#region constructor
		public new const string TableName = "Contact";
		public new const string ObjectName = "Contact";

		public Contact(int contactID)
			: base(TableName, ObjectName)
		{
			ContactID = contactID;
			Load();
		}

		public Contact()
			: base(TableName, ObjectName)
		{

		}
		#endregion constructor

		#region properties
		[ListingFieldAttribute("Contact ID")]
		[DataFieldAttribute("ContactID", false, true)]
		public int ContactID { get; set; }

		private int _clientID;
		[DataMember]
		[DataFieldAttribute("ClientID", false)]
		public int ClientID
		{
			get
			{
				return _clientID;
			}
			set
			{
				_clientID = value;
				_client = null;
			}
		}

		private Client _client;
		public Client ClientObj
		{
			get
			{
				if (_client == null && ClientID > -1)
				{
					_client = new Client(ClientID);
				}
				return _client;
			}
		}

		[ListingFieldAttribute("Client Name")]
		public string ClientName
		{
			get
			{
				return ClientObj.LastCommaFirstName;
			}
		}

		[DataMember]
		[DataFieldAttribute("FollowUpByDate", true)]
		public DateTime FollowupByDate { get; set; }

		public bool FollowUpWasNeeded
		{
			get
			{
				return (FollowupByDate != null);
			}
		}
		[ListingFieldAttribute("Additional Followup Needed")]
		public string FollowUpWasNeededString
		{
			get
			{
				return FollowUpWasNeeded ? "Was Needed" : "Was Not Needed";
			}
		}

		[DataMember]
		[DataFieldAttribute("UserID", false)]
		public int UserID { get; set; }
		//TODO: USER ID LIKE CLIENT ID

		[DataMember]
		[DataFieldAttribute("Type", false)]
		public int TypeInt { get; set; }
		public ContactTypes TypeEnum
		{
			get { return (ContactTypes)TypeInt; }
			set { TypeInt = (int)value; }
		}
		[ListingFieldAttribute("Type")]
		public string TypeStr
		{
			get
			{
				return TypeEnum.ToDisplayString();
			}
		}


		#endregion properties


		#region ObjectRepresentation implementations
		public override int GetPKValue()
		{
			return ContactID;
		}

		public override void SetPKValue(int pk)
		{
			ContactID = pk;
		}

		public override int GetNextPKValue()
		{
			int maxInt = 0;

			foreach (var item in DB.ReadObjectList(new Contact()))
			{
				maxInt = Math.Max(maxInt, item.GetPKValue());
			}
			return maxInt + 1;
		}

		public override void Load(int pk)
		{
			DB.ReadObject(this, pk);
		}

		public override void Update()
		{
			DB.UpdateObject(this, GetPKValue());
		}

		public override void Delete(int pk)
		{
			DB.DeleteObject(this, pk);
		}

		public override void Create()
		{
			DB.CreateObject(this);
		}

		public override ListItem ToListItem()
		{
			return new ListItem(ClientName, GetPKValue().ToString());
		}


		#endregion ObjectRepresentation implementations

		public static List<Contact> GetAll()
		{
			return DB.ReadObjectList(new Contact());
		}
	}
}
